home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / SuperMarquee-c / HexPerplex.c next >
Encoding:
Text File  |  1994-12-04  |  4.1 KB  |  161 lines  |  [TEXT/MMCC]

  1. //• NewMarquee.c
  2.  
  3. #define NIL 0L
  4.  
  5. BitMap    StringToBitMap (Str255 s, int h, int v, int descent);
  6. void    InitManagers (void);
  7. void MarqueeDisplay (Str255 s, int displayWidth);
  8. void MarqueeIdle (void);
  9. void MarqueeInit (void);
  10.  
  11. typedef struct MarqueeRec {
  12.     struct MarqueeRec    *fNext;
  13.     int            fType;
  14.     Rect        fFromRect, fToRect, newRect;
  15.     BitMap        fBits;
  16.     int            fOffset;
  17.     int            fLeftMaximum;
  18.     long        fDelayTimer;
  19.     WindowPtr    fMarqueePort;
  20. } MarqueeRec, *MarqueePtr;
  21.  
  22. QHdr    gMarqueeQueue;
  23.  
  24. main ()
  25. {
  26.     Rect        r;
  27.     WindowPtr    w;
  28.     EventRecord e;
  29.     Str255        s1 = "\p0 1 2 3 4 5 6 7 8 9 A B C D E F", 
  30.                 s2 = "\p0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F",
  31.                 s3 = "\p0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F",
  32.                 
  33.                 s4 = "\p0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F";
  34.     
  35.     InitManagers ();
  36.     MarqueeInit ();
  37.     SetRect (&r, 40, 40, 240, 190);
  38.     w = NewWindow (NIL, &r, "\pHex Perplex", true, rDocProc, (WindowPtr)-1L, false, 0);
  39.     SetPort (w);
  40.     TextFont (monaco);
  41.     MoveTo (100, 30);
  42.     MarqueeDisplay (s1, 80);
  43.     MoveTo (100, 60);
  44.     MarqueeDisplay (s2, 80);
  45.     MoveTo (100, 90);
  46.     MarqueeDisplay (s3, 80);
  47.     MoveTo (100, 120);
  48.     MarqueeDisplay (s4, 80);
  49.     while (! GetNextEvent (mDownMask, &e) )
  50.         MarqueeIdle ();
  51. }
  52.  
  53. void    InitManagers (void)
  54. {
  55.     InitGraf (&qd.thePort);
  56.     InitFonts ();
  57.     InitWindows ();
  58.     InitMenus ();
  59.     TEInit ();
  60.     InitDialogs (NIL);
  61.     FlushEvents (everyEvent, 0);
  62.     InitCursor ();
  63. }
  64.  
  65. /***************************/
  66. BitMap    StringToBitMap (Str255 s, int h, int v, int descent)
  67. {
  68.     GrafPtr        saved;
  69.     GrafPort    newPort;
  70.     Rect        r;
  71.  
  72.     GetPort (&saved);
  73.     OpenPort (&newPort);
  74.     SetRect (&r, 0, 0, h, v);
  75.     BlockMove (&r, &newPort.portRect, sizeof (Rect));
  76.  
  77.     RectRgn (newPort.visRgn, &r);
  78.     RectRgn (newPort.clipRgn, &r);
  79.     newPort.portBits.rowBytes = ((h + 15) / 16) * 2;
  80.     newPort.portBits.baseAddr = NewPtr (v * newPort.portBits.rowBytes );
  81.     BlockMove (&r, &newPort.portBits.bounds, sizeof (Rect));
  82.  
  83.     EraseRect (&r);
  84.     MoveTo (0, v - descent);
  85.     TextFont (saved->txFont);
  86.     TextFace (saved->txFace);
  87.     TextSize (saved->txSize);
  88.     DrawString (s);
  89.  
  90.     SetPort (saved);
  91.     return newPort.portBits;
  92. }
  93. /***************************/
  94. void MarqueeInit ()
  95. {
  96.   gMarqueeQueue.qFlags = 0;
  97.   gMarqueeQueue.qHead = NIL;
  98.   gMarqueeQueue.qTail = NIL;
  99. }  
  100. /***************************/
  101. void MarqueeIdle (void )
  102. {
  103.     MarqueePtr    thisRec;
  104.     
  105.     thisRec = (MarqueePtr) gMarqueeQueue.qHead;
  106.     for (; thisRec != NIL; thisRec = thisRec->fNext )
  107.         if (thisRec->fOffset != 0 && TickCount () > thisRec->fDelayTimer)
  108.             {
  109.             thisRec->fDelayTimer = TickCount () + 1;
  110.             
  111.             CopyBits (&thisRec->fBits, &thisRec->fMarqueePort->portBits, &thisRec->fFromRect, &thisRec->fToRect, srcCopy, NIL);
  112.             
  113.             if (thisRec->fFromRect.left + thisRec->fOffset < 0 || thisRec->fFromRect.left + thisRec->fOffset > thisRec->fLeftMaximum) 
  114.                 {
  115.                 thisRec->fOffset = -thisRec->fOffset;
  116.                 thisRec->fDelayTimer = TickCount () + 40;
  117.                 }
  118.             else
  119.                 OffsetRect (&thisRec->fFromRect, thisRec->fOffset, 0);
  120.             }
  121. }
  122. /***************************/
  123. void MarqueeDisplay (Str255 s, int displayWidth)
  124. {
  125.     int            sWidth, dH, lineHeight;
  126.     Point        curLoc;
  127.     FontInfo    fontstuff;
  128.     MarqueePtr    newRec;
  129.     
  130.     sWidth = StringWidth (s);
  131.     if (sWidth < displayWidth) 
  132.     {
  133.         Move (- (sWidth / 2), 0);
  134.         DrawString (s);
  135.      }
  136.     else
  137.         {
  138.             newRec = (MarqueePtr) (NewPtrClear (sizeof (MarqueeRec) + 4) + 4);
  139.             
  140.             GetFontInfo (&fontstuff);
  141.             
  142.             lineHeight = fontstuff.ascent + fontstuff.descent + fontstuff.leading;
  143.             newRec->fBits = StringToBitMap (s, sWidth, lineHeight, fontstuff.descent);
  144.             
  145.             SetRect (&newRec->fFromRect, 0, 0, displayWidth, lineHeight);
  146.             
  147.             BlockMove (&newRec->fFromRect, &newRec->fToRect, sizeof (Rect));
  148.             GetPen (&curLoc);
  149.             OffsetRect (&newRec->fToRect, curLoc.h - displayWidth / 2, curLoc.v - fontstuff.ascent);
  150.             InsetRect (&newRec->fToRect, -2, -2);
  151.             FrameRect (&newRec->fToRect);
  152.             InsetRect (&newRec->fToRect, 2, 2);
  153.             newRec->fLeftMaximum = sWidth - displayWidth;
  154.             newRec->fOffset = 1;
  155.             newRec->fDelayTimer = TickCount () + 1;
  156.             GetPort (&newRec->fMarqueePort);
  157.             
  158.             Enqueue ((QElemPtr) newRec, &gMarqueeQueue );
  159.      }
  160. }
  161.